<template>
  <div class="app-container">
    <div class="search">
      <p class="float-left mr-10 mb-10" style="width: 200px">
        <el-input v-model="searchParams.account" placeholder="请输入手机号"></el-input>
      </p>
      <p class="float-left mr-10 mb-10">
        <el-button type="primary" @click="searchParty">搜索</el-button>
        <el-button type="primary" @click="resetParty">重置</el-button>
      </p>
    </div>
    <div class="my-10">
      <bdButton :handlerBtn="handlerBtn"/>
    </div>
    <bdTable :tableHeader="tableHeader" :tableData="tableData" class="mb-10" />
    <bd-page
      :pageSize="pages"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></bd-page>
    <addPartyAndEditParty ref="useParty" @update:visible="searchParty(true)"></addPartyAndEditParty>
  </div>
</template>
<script>
import addPartyAndEditParty from './addPartyAndEditParty.vue'
import { getUsers, deleteUsers } from '@/api/users'
import { mapGetters } from 'vuex'
const searchParams = {
  account: null,
  isDy: true
}
export default {
  name: 'rightTable',
  components: {
    addPartyAndEditParty
  },
  data () {
    return {
      searchParams: Object.assign({}, searchParams),
      options: [
        {
          value: 0,
          label: '禁用'
        },
        {
          label: '激活',
          value: 1
        }
      ],
      handlerBtn: [
        {
          id: 1,
          name: '新增党员',
          handler: this.addParty
        }
      ],
      tableHeader: {
        type: 'selection',
        header: [
          {
            prop: 'name',
            label: '姓名'
          },
          {
            prop: 'phone',
            label: '电话',
            minWidth: 110
          },
          {
            prop: 'department',
            label: '所属社区',
            minWidth: 180
          },
          {
            prop: 'sfzh',
            minWidth: 180,
            label: '所属党支部'
          },
          {
            prop: 'sex',
            label: '性别',
            filter: (row) => {
              return row.sex ? '女' : '男'
            }
          },
          {
            prop: 'createTime',
            label: '创建日期',
            minWidth: 150
          },
          {
            label: '操作',
            type: 'button',
            width: 150,
            btn: [
              {
                id: 1,
                name: '编辑',
                handler: this.handleEdit
              },
              {
                id: 2,
                name: '删除',
                type: 'danger',
                handler: this.handleDelete
              }
            ]
          }
        ]
      },
      // 列表数据
      tableData: [],
      // 分页
      pages: {
        page: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  computed: {
    ...mapGetters(['user'])
  },
  mounted () {
    this.searchParty()
  },
  methods: {
    async searchParty (clearPage = false) {
      if (clearPage) {
        this.pages.page = 1
        this.pages.pageSize = 10
      }
      const params = {
        ...this.pages,
        ...this.searchParams,
        departmentId: this.user.departmentId
      }
      const { total, list } = await getUsers(params)
      this.tableData = list
      this.total = total
    },
    resetParty () {
      this.searchParams = Object.assign({}, searchParams)
      this.searchParty(true)
    },
    // 新增用户
    addParty () {
      this.$refs.useParty.isShow()
    },
    // 编辑用户
    handleEdit (e, row) {
      this.$refs.useParty.isShow(true, row)
    },
    // 删除用户
    handleDelete (e, row) {
      this.$confirm('此操作将删除社区, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteUsers({ id: row.id })
        this.searchParty(true)
      }).catch(() => {
      })
    },
    // 条数
    handleSizeChange (val) {
      this.pages.pageSize = val
      this.searchParty()
    },
    // 页数
    handleCurrentChange (val) {
      this.pages.page = val
      this.searchParty()
    }
  }
}
</script>
<style lang="scss" scoped>
.my-10 {
  margin: 10px 0;
}
.mb-10 {
  margin-bottom: 10px;
}
.search {
  display: flex;
  flex-wrap: wrap;
}
.float-left {
  float: left;
}
.mr-10 {
  margin-right: 10px;
}
</style>
